package com.china.bike.service.impl;

import com.china.bike.dao.BikeAccountDao;
import com.china.bike.dao.BikeAccountRecordDao;
import com.china.bike.dao.UserDao;
import com.china.bike.entity.*;
import com.china.bike.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private BikeAccountDao bikeAccountDao;
    @Autowired
    private BikeAccountRecordDao bikeAccountRecordDao;

    @Override
    public int createUser(User user) {
        User result = userDao.searchUserByCondition(user);
        if (result != null) {
            return 0;
        }
        user.setRegisterDate(new Date());
        userDao.createUser(user);

        User userByCondition = userDao.searchUserByCondition(user);
        BikeAccount bikeAccount = new BikeAccount();
        bikeAccount.setUserId(userByCondition.getId());
        bikeAccount.setIsDeposit((byte) 1);
        bikeAccount.setDepositAmount(new BigDecimal("299.00"));
        bikeAccountDao.createBikeAccount(bikeAccount);

        BikeAccountRecord bikeAccountRecord = new BikeAccountRecord();
        bikeAccountRecord.setType(2);
        bikeAccountRecord.setUserId(userByCondition.getId());
        bikeAccountRecord.setAmount(new BigDecimal("299.00"));
        bikeAccountRecordDao.createAccountRecord(bikeAccountRecord);
        return 1;
    }

    @Override
    public User searchUser(User user) {
        return userDao.searchUserByCondition(user);
    }

    @Override
    public List<BikeUseRecord> searchUserJourney(User user) {
        return userDao.searchUserJourney(user);
    }

    @Override
    public List<BikeRepairRecord> searchBikeRepairRecord(User user) {
        return userDao.searchBikeRepairRecord(user);
    }
}
